<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NoticeModel;
use app\zhongcan\validate\NoticeValidate;
use cmf\controller\AdminBaseController;
use think\Request;

class NoticeController extends AdminBaseController
{
    //院部首页
    public function index()
    {
        $data = input('title_author');
        $where = [];
        if (!empty($data)){
            $where[] = ['title|author','like',"%$data%"];
        }
        $notice = NoticeModel::order(['list_order' => 'desc','id'=>'desc'])
            ->where($where)
            ->paginate(2);
        $notice->appends(request()->param());
        $this->assign('notice',$notice);
        return $this->fetch();
    }
    //添加页面
    public function add()
    {
        return $this->fetch();
    }
    //添加功能
    public function addPost(Request $request)
    {

        $data = $request->param();
        //验证器验证
        $validate = new NoticeValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        $data['content'] = htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($data['content'])));
        $data['create_time'] = date('Y-m-d H:i:s');
        $add = NoticeModel::create($data);
        if($add){
            $this->success('添加成功','Notice/index');

        }else{
            $this->error('添加失败');
        }
    }
    //修改页面
    public function edit()
    {
        $id = input('id/d');
        $data = NoticeModel::find($id);

        $data['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
        return $this->fetch('edit',[
            'notice' => $data
        ]);

    }
    //修改功能
    public function editPost(Request $request)
    {
        $data = $request->param();
        //验证器验证
        $validate = new NoticeValidate();
        $res = $validate->scene('edit')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        $data['content'] = htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($data['content'])));
        $notice = NoticeModel::find($data['id']);
        if (!$notice){
            $this->error('未查询到数据');
        }
        $notice -> title = $data['title'];
        $notice -> image = $data['image'];
        $notice ->abstract = $data['abstract'];
        $notice -> author = $data['author'];
        $notice -> content = $data['content'];
        if ($notice->save()){
            $this->success('保存成功','Notice/index');
        }else{
            $this->error('保存失败');
        }
    }
    //删除功能
    public function del()
    {
        $id = input('id');
        $del = NoticeModel::destroy($id);
        if ($del){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //排序
    public function ListOrder()
    {
        $val = parent::listOrders(new NoticeModel());
        if ($val){
            $this->success('排序成功');
        }else{
            $this->error('排序失败');
        }

    }
    //修改状态
    public function editSwitch()
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('错误');
        }
        $data = NoticeModel::find($id);
        if (!$data){
            $this->error('错误');
        }
        if ($data['switch'] == 0){
            $data->switch = 1;
        }else{
            $data->switch = 0;
        }
        if ($data->save()){
            $this->success('成功');
        }else{
            $this->error('失败');
        }


    }
    //预览文章
    public function getContent()
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('错误');
        }
        $data = NoticeModel::find($id);
        if (!$data){
            $this->error('错误');
        }
        $data['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
        return $this->fetch('content',[
            'data'=>$data
        ]);
    }

}